package com.wing.res.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.service.DynamicService;
import com.wing.res.dao.ResWorkSiteDao;
import com.wing.res.model.entity.ResWorkSite;
import com.wing.res.model.entity.ResWorkStation;
import com.wing.res.model.request.ResWorkSiteUpdateForm;
import com.wing.res.model.response.ResWorkSiteSearchVo;
import com.wing.res.service.ResWorkSiteService;
import com.wing.web.utils.JwtUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 工位-ServiceImpl
 *
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-19 15:33
 **/
@Service
public class ResWorkSiteServiceImpl extends ServiceImpl<ResWorkSiteDao, ResWorkSite> implements ResWorkSiteService {

    @Resource
    private DynamicService dynamicService;

    /**
     * 查询工位信息
     *
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<ResWorkSiteSearchVo> pageList4app(Page<ResWorkSiteSearchVo> pg, Map<String, Object> map) {
        List<ResWorkSiteSearchVo> vos = this.baseMapper.pageList4app(pg, map);
        pg.setRecords(vos);
        return pg;
    }

    /**
     * 禁用
     * @param id
     * @return
     */
    @Override
    public int cancellation(String id) {
        ResWorkSite resWorkSite = this.baseMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(resWorkSite)) {
            resWorkSite.setIsDeleted(true);
            resWorkSite.setUpdateTime(LocalDateTime.now());
            resWorkSite.setModifier(JwtUtils.getLoginUserId());
            resWorkSite.setModifierName(JwtUtils.getLoginUsername());
        }
        return baseMapper.updateById(resWorkSite);
    }

    /**
     * 启用
     * @param id
     * @return
     */
    @Override
    public int recover(String id) {
        ResWorkSite resWorkSite = this.baseMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(resWorkSite)) {
            resWorkSite.setIsDeleted(false);
            resWorkSite.setUpdateTime(LocalDateTime.now());
            resWorkSite.setModifier(JwtUtils.getLoginUserId());
            resWorkSite.setModifierName(JwtUtils.getLoginUsername());
        }
        return baseMapper.updateById(resWorkSite);
    }

    /**
     * 修改工站信息
     * @param form
     * @return
     */
    @Override
    public int updateWorkSiteBy(ResWorkSiteUpdateForm form) {
        Long id = form.getId();
        ResWorkSite resWorkSite = this.baseMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(resWorkSite)) {
            BeanUtil.copyProperties(form, resWorkSite);
            resWorkSite.setUpdateTime(LocalDateTime.now());
            resWorkSite.setModifier(JwtUtils.getLoginUserId());
            resWorkSite.setModifierName(JwtUtils.getLoginUsername());
            int i = this.baseMapper.updateById(resWorkSite);
            if (i > 0) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 添加工站信息
     * @param form
     * @return
     */
    @Override
    public int addWorkSiteBy(ResWorkSiteUpdateForm form) {
        ResWorkSite resWorksite = new ResWorkSite();
        //生成工位编号
        String taskNo = UniqueIdGeneratorUtil.generateUniqueId("GZ", "yyyyMMdd", 1000, 9999);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String code=dynamicService.getDayNextCode("res_work_site");
        String day=simpleDateFormat.format(new Date());
        BeanUtil.copyProperties(form, resWorksite);
        resWorksite.setCreateTime(LocalDateTime.now());
        resWorksite.setCreator(JwtUtils.getLoginUserId());
        resWorksite.setCreatorName(JwtUtils.getLoginUsername());
        if (StrUtil.isNotEmpty(form.getCode())){
            resWorksite.setCode(taskNo);
        }
        resWorksite.setNo(Integer.parseInt(code));
        resWorksite.setDay(Integer.parseInt(day));
        resWorksite.setUpdateTime(LocalDateTime.now());
        int insert = this.baseMapper.insert(resWorksite);
        if (insert>0){
            return 1;
        }
        return 0;
    }
}
